;(function (window,$) {
    // 创建切换DOM对象
    function ToggleDom(options) {
        return new ToggleDom.prototype.init(this,options);
    }
    ToggleDom.prototype = {
        constructor:ToggleDom,
        init:function (eventDomSelector,options) {
            this.eventDoms = eventDomSelector;
            this.addElm = $(options.addDomSelector);
            this.cloneDom =  options.cloneDomSelector || undefined;
            this.result = [];
            this.toggleLogic(options);
        },
        toggleLogic:function (options) {
            var that = this;

            // 判断是否已添加 非常重要此属性  由于该插件使用了事件委托而使用该插件很有可能Utils对象是一个不存在的DOM元素
            // 所以需要用这个进行判断 而不能使用使用原生做案例时的方法 给添加事件的元素绑定一个flag了
            var isExists = {};

            $(options.delegateDom).delegate("click",this.eventDoms.selector,function (e) {
                if (!isExists.hasOwnProperty($(this).index())){
                    isExists[$(this).index()] = "";
                    var uId = (Math.random()*0xFFFFFF).toString(16);
                    var addDom;
                    var cloneDom = $(that.cloneDom);
                    var dom = cloneDom.eq($(this).index()).get(0);
                    dom.id = uId;
                    that.result.push(dom);
                    addDom = cloneDom.eq($(this).index()).clone(true);
                    addDom.get(0).id = uId
                    addDom.addClass(" clone");
                    that.addElm.append(addDom);
                }else{
                    delete isExists[$(this).index()];
                    var  cloneDom2 = $(that.cloneDom).eq($(this).index()).get(0);
                    var clones = $(".clone");
                    that.result.splice(that.result.indexOf(cloneDom2),1);
                    clones.each(function (index,dom) {
                        if (cloneDom2.id === dom.id){
                            that.addElm.get(0).removeChild(dom);
                        }
                    });
                }
                options.addDom(that.result);
            });

            // this.eventDoms.each(function (index,dom) {
            //     dom.flag = "false";
            // });
            //
            // this.eventDoms.click(function () {
            //     if (this.flag === "false"){
            //         this.flag = "true";
            //         var uId = (Math.random()*0xFFFFFF).toString(16);
            //         var addDom;
            //         if (that.cloneDom){
            //             var cloneDom = $(that.cloneDom);
            //             var dom = cloneDom.eq($(this).index()).get(0);
            //             dom.id = uId;
            //             that.result.push(dom);
            //             addDom = cloneDom.eq($(this).index()).clone(true);
            //             addDom.get(0).id = uId
            //         }
            //         if(that.createDom){
            //             addDom = $.create(options.createDom.tagName,
            //                 cloneDom.eq($(this).index()).text(),options.createDom.attrs);
            //             addDom.css(options.createDom.styles);
            //             addDom.get(0).id = uId;
            //             if (options.createDom.bindClick){
            //                 addDom.click(function () {
            //                     $(this).remove();
            //                 });
            //             }
            //         }
            //         addDom.addClass(" clone");
            //         that.addElm.append(addDom);
            //     }else{
            //         this.flag = "false";
            //         var  cloneDom2 = $(that.cloneDom).eq($(this).index()).get(0);
            //         var clones = $(".clone");
            //         that.result.splice(that.result.indexOf(cloneDom2),1);
            //         clones.each(function (index,dom) {
            //             if (cloneDom2.id === dom.id){
            //                 that.addElm.get(0).removeChild(dom);
            //             }
            //         });
            //     }
            //     options.addDom(that.result);
            // });
        }
    };
    ToggleDom.prototype.init.prototype = ToggleDom.prototype;
    // 给Utils进行扩展
    $.fn.extends({toggleDom:ToggleDom})
})(window,Util);


// $(".ul-item-img").toggleDom({
//     addDomSelector:".addItemList-ul",
//     delegateDom:".listItem",
//     cloneDomSelector:".listItem-ul-item",
//     addDom:function (res) {
//         console.log(res)
//     }
// });